package com.ziyun.erp.web.api.goods;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.modules.goods.entity.GoodsTemplateEntity;
import com.ziyun.erp.modules.goods.service.GoodsTemplateService;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryEntity;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryVO;
import com.ziyun.erp.modules.goodsCategory.service.GoodsCategoryService;

import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;

/**
 * 商品分类表
 *
 * @author
 * @email
 * @date 2018-08-17 13:22:48
 */
@RestController
@RequestMapping("port/goodsCategory/goodscategory")
public class GoodsCategoryControllerApi {
	@Autowired
	private GoodsCategoryService goodsCategoryService;

	@Autowired
	private GoodsTemplateService goodsTemplateService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public List<GoodsCategoryEntity> list(@RequestParam Map<String, Object> params) {
		List<GoodsCategoryEntity> goodsCategoryEntityList = goodsCategoryService
				.selectList(new EntityWrapper<GoodsCategoryEntity>().eq("disabled", 0));
		for (GoodsCategoryEntity goodsCategoryEntity : goodsCategoryEntityList) {
			GoodsCategoryEntity parentGoodsCategoryEntity = goodsCategoryService
					.selectById(goodsCategoryEntity.getParentCategoryId());
			if (parentGoodsCategoryEntity != null) {
				goodsCategoryEntity.setParentName(parentGoodsCategoryEntity.getCategoryName());
			}
		}

		return goodsCategoryEntityList;
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{categoryId}")
	public R info(@PathVariable("categoryId") Integer categoryId) {
		return goodsCategoryService.info(categoryId);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody GoodsCategoryEntity goodsCategory) {
		goodsCategoryService.insertGoodsCategoryAndCategoryRelation(goodsCategory);
		return R.ok();
	}

	/**
	 * 修改-->废弃
	 */
	@RequestMapping("/update")
	public R update(@RequestBody GoodsCategoryEntity goodsCategory) {
		ValidatorUtils.validateEntity(goodsCategory);
		goodsCategoryService.updateAllColumnById(goodsCategory);// 全部更新
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] categoryIds) {
		goodsCategoryService.deleteBatchIds(Arrays.asList(categoryIds));
		return R.ok();
	}

	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	public R select() {
		List<GoodsCategoryEntity> goodsCategoryEntityList = goodsCategoryService.select();
		return R.ok().put("goodsCategoryEntityList", goodsCategoryEntityList);
	}

	/**
	 * 按categoryId获得其子分类集合
	 */
	@RequestMapping("/getSonCategory")
	public R getSonCategory(@RequestBody Map categoryMap) {
		Integer categoryId = (Integer) categoryMap.get("categoryId");
		HashMap<String, Object> map = new HashMap<>();
		map.put("disabled", 0);
		map.put("parent_category_id", categoryId);
		List<GoodsCategoryEntity> goodsCategoryList = goodsCategoryService.selectByMap(map);

		return R.ok().put("goodsCategoryList", goodsCategoryList);
	}

	/**
	 * 按categoryId获得三级分类全称和商品模板对象
	 */
	@RequestMapping("/getCatNames")
	public R getCatNames(@RequestBody Map paramBodyMap) {
		Map params = (Map) paramBodyMap.get("params");
		Integer categoryId = (Integer) params.get("categoryId");

//		List<GoodsCategoryEntity> goodsCategoryList = goodsCategoryService.getCatNamesByCategoryId(categoryId);
//		StringBuffer categoryNames = new StringBuffer();
//		for (GoodsCategoryEntity goodsCategoryEntity : goodsCategoryList) {
//			categoryNames.append(goodsCategoryEntity.getCategoryName());
//			categoryNames.append("->");
//		}
//		GoodsCategoryEntity categoryEntity = goodsCategoryService.selectById(categoryId);
//		categoryNames.append(categoryEntity.getCategoryName());
//		// 根据分类id获得商品模板对象
//		GoodsTemplateEntity goodTemplate = goodsTemplateService.getTemplateByCatId(categoryId);
//		// 判断模板对象是否含有工艺选项
//		String attributeTypes = goodsTemplateService.getAttributeTypes(goodTemplate.getTemplateId());
//		if (attributeTypes.contains("craft")) {
//			goodTemplate.setIncludCraft(true);
//		} else {
//			goodTemplate.setIncludCraft(false);
//		}
		return goodsCategoryService.getCatNamesByCategoryId(categoryId);
	}

	/**
	 * 接收表单数据,更新goods_category和goods_category_relation
	 */
	@PostMapping("updateCategory")
	public R updateCategory(GoodsCategoryVO goodsCategoryVO) {
		goodsCategoryService.updateGoodsCategoryAndCategoryRelationById(goodsCategoryVO);
		return R.ok();
	}

	/**
	 * 获取商品类型枚举
	 */
	@GetMapping("/getGoodsType")
	public R getGoodsType() {
		return goodsCategoryService.getGoodsType();
	}

	/**
	 * 获取商品分类树
	 * 
	 * @return
	 */
	@RequestMapping("/getCategoryTree")
	public R getAddInfo() {
		Map addInfoMap = goodsCategoryService.getAddInfo();
		return R.ok().put("addInfo", addInfoMap);
	}

	/**
	 *
	 */
	@RequestMapping("/countPrice")
	public R countPrice(@RequestBody Map paramBodyMap) {
		Map<String, Object> map = goodsCategoryService.countPrice(paramBodyMap);
		return R.ok().put("map",map);
	}
}
